﻿using ETask.Api.Data;
using ETask.Api.Dtos;
using ETask.Api.Models;
using Microsoft.EntityFrameworkCore;

namespace ETask.Api.Repositories
{
    public class DeptRepository : Repository<Dept>, IDeptRepository
    {
        public DeptRepository(AppDbContext context) : base(context)
        {
        }

        public async Task<PagedResult<DeptDto>> GetListAsync(DeptListParams listParams)
        {
            var query = _context.Depts.AsQueryable();
            if (!string.IsNullOrWhiteSpace(listParams.KeyWord))
            {
                query = query.Where(q => q.DeptCode.Contains(listParams.KeyWord) || q.DeptName.Contains(listParams.KeyWord));
            }
            return await query
                .Select(q => new DeptDto
                {
                    Id = q.Id,
                    DeptCode = q.DeptCode,
                    DeptName = q.DeptName,
                    CreatedAt = q.CreatedAt,
                    UpdatedAt = q.UpdatedAt,
                }).ToPagedResultAsync(listParams.PageDto);
        }
    }
}
